package com.LCQ.crm.workbench.controller;
import com.LCQ.crm.settings.domain.User;
import com.LCQ.crm.settings.service.UserService;
import com.LCQ.crm.settings.service.UserServiceImpl;
import com.LCQ.crm.utils.DateTimeUtil;
import com.LCQ.crm.utils.PrintJson;
import com.LCQ.crm.utils.ServiceFactory;
import com.LCQ.crm.utils.UUIDUtil;
import com.LCQ.crm.vo.PaginationVo;
import com.LCQ.crm.workbench.domain.Contacts;
import com.LCQ.crm.workbench.domain.Tran;
import com.LCQ.crm.workbench.domain.TranHistory;
import com.LCQ.crm.workbench.service.TransactionService;
import com.LCQ.crm.workbench.service.impl.TransactionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TransactionServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("!!!!TransactionService!!!!");
        String reqUri = request.getRequestURI();

        if(reqUri==null){
            return;
        }

        if(reqUri.contains("getCustomerName.do")){
            getCustomerName(request,response);
            return;
        }else if(reqUri.contains("savePrepare.do")) {
            savePrepare(request, response);
            return;
        }else if(reqUri.contains("save.do")) {
            save(request, response);
            return;
        }else if(reqUri.contains("getActivityByCond.do")) {
            getActivityByCond(request, response);
            return;
        }else if(reqUri.contains("getContactsByCond.do")) {
            getContactsByCond(request, response);
            return;
        }else if(reqUri.contains("getPageList.do")) {
            getPageList(request, response);
            return;
        }else if(reqUri.contains("detail.do")) {
            detail(request, response);
            return;
        }else if(reqUri.contains("getTranHistoryList.do")) {
            getTranHistoryList(request, response);
            return;
        }else if(reqUri.contains("changeStage.do")) {
            changeStage(request, response);
            return;
        }



    }

    private void changeStage(HttpServletRequest request, HttpServletResponse response) {
        String stage = request.getParameter("stage");
        String tranId = request.getParameter("id");
        TransactionService transactionService = (TransactionService)ServiceFactory.getService(new TransactionServiceImpl());
        boolean result = transactionService.changeStage(stage,tranId,((User)request.getSession().getAttribute("user")).getName());
        if(result){
            PrintJson.printJsonFlag(response,true);
        }else {
            PrintJson.printJsonFlag(response,false);
        }
    }

    private void getTranHistoryList(HttpServletRequest request, HttpServletResponse response) {
        String tranId = request.getParameter("id");
        TransactionService transactionService = (TransactionService)ServiceFactory.getService(new TransactionServiceImpl());
        List<TranHistory> list = transactionService.getTranHistoryList(tranId);
        Map<String,String> map = (Map<String,String>)request.getServletContext().getAttribute("stage2Possibility");
        for(TranHistory th:list){
            th.setPossibility(map.get(th.getStage()));
        }
        PrintJson.printJsonObj(response,list);
    }

    private void detail(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        TransactionService transactionService = (TransactionService)ServiceFactory.getService(new TransactionServiceImpl());
        Tran tran = transactionService.getDetail(id);
        Map<String,String> map = (Map<String,String>)request.getServletContext().getAttribute("stage2Possibility");
        tran.setPossibility(map.get(tran.getStage()));
        request.setAttribute("tran",tran);
        try {
            request.getRequestDispatcher("/workbench/transaction/detail.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getPageList(HttpServletRequest request, HttpServletResponse response) {
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        String owner = request.getParameter("owner");
        String name = request.getParameter("name");
        String customerName = request.getParameter("customerName");
        String stage = request.getParameter("stage");
        String source = request.getParameter("source");
        String contactsName = request.getParameter("contactsName");
        Map<String,Object> map = new HashMap<>();
        map.put("index",(pageNum-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("owner",owner);
        map.put("name",name);
        map.put("customerName",customerName);
        map.put("stage",stage);
        map.put("source",source);
        map.put("contactsName",contactsName);
        TransactionService transactionService = (TransactionService)ServiceFactory.getService(new TransactionServiceImpl());
        PaginationVo<Tran> page = transactionService.getPageList(map);
        PrintJson.printJsonObj(response,page);
    }

    private void getContactsByCond(HttpServletRequest request, HttpServletResponse response) {
        TransactionService transactionService = (TransactionService)ServiceFactory.getService(new TransactionServiceImpl());
        String name = request.getParameter("name");
        Map<String,String> map = new HashMap<>();
        map.put("name",name);
        List<Contacts> list = transactionService.getContactsByCond(map);
        PrintJson.printJsonObj(response,list);
    }

    private void getActivityByCond(HttpServletRequest request, HttpServletResponse response) {
        TransactionService transactionService = (TransactionService)ServiceFactory.getService(new TransactionServiceImpl());
        String name = request.getParameter("name");
        Map<String,String> map = new HashMap<>();
        map.put("name",name);
        List<Contacts> list = transactionService.getActivityByCond(map);
        PrintJson.printJsonObj(response,list);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) {
        TransactionService transactionService = (TransactionService)ServiceFactory.getService(new TransactionServiceImpl());
        String owner = request.getParameter("owner");
        String money = request.getParameter("money");
        String transactionName = request.getParameter("transactionName");
        String expectedDate = request.getParameter("expectedDate");
        String customerName = request.getParameter("customerName");
        String stage = request.getParameter("stage");
        String type = request.getParameter("type");
        String source = request.getParameter("source");
        String activityId = request.getParameter("activityId");
        String contactsId = request.getParameter("contactsId");
        String description = request.getParameter("description");
        String contactSummary = request.getParameter("contactSummary");
        String nextContactTime = request.getParameter("nextContactTime");

        Tran tran = new Tran();
        tran.setName(transactionName);
        tran.setExpectedDate(expectedDate);
        tran.setStage(stage);
        tran.setType(type);
        tran.setSource(source);
        tran.setActivityId(activityId);
        tran.setContactsId(contactsId);
        tran.setDescription(description);
        tran.setContactSummary(contactSummary);
        tran.setNextContactTime(nextContactTime);
        tran.setOwner(owner);
        tran.setMoney(money);
        tran.setId(UUIDUtil.getUUID());
        tran.setCreateBy(((User)request.getSession().getAttribute("user")).getName());
        tran.setCreateTime(DateTimeUtil.getSysTime());
        boolean result = transactionService.save(tran,customerName);
        if(result){
            PrintJson.printJsonFlag(response,true);
        }else {
            PrintJson.printJsonFlag(response,false);
        }

    }

    private void savePrepare(HttpServletRequest request, HttpServletResponse response) {
        UserService userService = (UserService)ServiceFactory.getService(new UserServiceImpl());
        List<Map> list = userService.selectAll();
        request.setAttribute("userList",list);
        try {
            request.getRequestDispatcher("/workbench/transaction/save.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getCustomerName(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        TransactionService transactionService = (TransactionService)ServiceFactory.getService(new TransactionServiceImpl());
        String[] names = transactionService.getCustomerName(name);
        PrintJson.printJsonObj(response,names);
    }
}
